package com.bjdd.jdy;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by yukai on 2016/11/12.
 */

public class App extends Application{
    private static App app;
    private Activity mCurrentActivity;

    private static List<Activity> mList = new LinkedList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        SDKInitializer.initialize(this);
        SpUtil.init(this);
        //JPush
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        //友盟
        MobclickAgent.openActivityDurationTrack(false);
        //日志加密
        MobclickAgent.enableEncrypt(true);
    }



    public static Context getAppContext() {
        return app;
    }

    public static Resources getAppResources() {
        return app.getResources();
    }

    public static App getApp(){return app;}

    public boolean hasLocation(){
        LocationManager lm=(LocationManager) getSystemService(LOCATION_SERVICE);
        boolean ok=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        return ok;
    }

    // add Activity
    public void addActivity(Activity activity) {
        if(!mList.contains(activity)) {
            mList.add(activity);
        }else{
            mList.remove(activity);
            mList.add(activity);
        }
    }
    // remove Activity
    public void removeActivity(Activity activity) {
        mList.remove(activity);
    }

    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MobclickAgent.onKillProcess(getApplicationContext());
            System.exit(0);
        }
    }

    public static Activity getCurrentActivity() {
        L.d("当前activity："+mList.size());
        return mList.get(mList.size()-1);
    }


}
